using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Runtime.InteropServices.WindowsRuntime;
using DevCangjie.utils.Settings;
using Microsoft.UI.Xaml;
using Microsoft.UI.Xaml.Controls;
using Microsoft.UI.Xaml.Controls.Primitives;
using Microsoft.UI.Xaml.Data;
using Microsoft.UI.Xaml.Input;
using Microsoft.UI.Xaml.Media;
using Microsoft.UI.Xaml.Navigation;
using Windows.Foundation;
using Windows.Foundation.Collections;

// To learn more about WinUI, the WinUI project structure,
// and more about our project templates, see: http://aka.ms/winui-project-info.

namespace DevCangjie.Pages.ConsolePage
{
    public sealed partial class ConsolePage : Page
    {
        public static ConsolePage Instance { get; private set; } = new ConsolePage();

        public OutputPage outputPage = new OutputPage();
        private ErrorListPage errorListPage = new ErrorListPage();
        private DevCangjie.Pages.TerminalPage.TerminalPage terminalPage = new DevCangjie.Pages.TerminalPage.TerminalPage();

        public ConsolePage()
        {
            this.InitializeComponent();

            Instance = this;

            // 设置默认显示的页面为已经创建的实例
            ContentFrame.Content = outputPage;
            ConsoleNavView.SelectedItem = ConsoleNavView.MenuItems[0];
        }

        private void NavigationView_SelectionChanged(object sender, NavigationViewSelectionChangedEventArgs e)
        {
            var selectedItem = e.SelectedItem as NavigationViewItem;

            if (selectedItem != null)
            {
                string tag = selectedItem.Tag?.ToString();

                switch (tag)
                {
                    case "Output":
                        ContentFrame.Content = outputPage;
                        break;
                    case "ErrorList":
                        ContentFrame.Content = errorListPage;
                        break;
                    case "Terminal":
                        ContentFrame.Content = terminalPage;
                        break;
                }
            }
        }

        public void ApplySettings()
        {
            outputPage.RefreshUI();
            errorListPage.RefreshUI();
        }

        public void AppendOutput(string text)
        {
            outputPage.AppendOutput(text + "\n");
        }

        public void AppendErrorOutput(string text)
        {
            outputPage.AppendOutput("[ERROR] " + text + "\n");
        }

        public void AppendWarningOutput(string text)
        {
            outputPage.AppendOutput("[WARNING] " + text + "\n");
        }

        public void AppendInfoOutput(string text)
        {
            outputPage.AppendOutput("[INFO] " + text + "\n");
        }
    }
}
